package org.vectrics.company;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.vectrics.SystemException;
import org.vectrics.common.db.Expirable;
import org.vectrics.common.db.RecordObject;
import org.vectrics.common.db.Voidable;
import org.vectrics.user.Person;


public class CompanyPersonRole implements RecordObject, Expirable, Voidable {
	public static int MAX_LENGTH_TITLE = 60;
	private Long id = null;
    private CompanyPersonRoleType roleType = null;
    private String title = null;
    private Date dateStart = null;
    private Date dateEnd = null;
    private Date dateStartExpected = null;
    private Date dateEndExpected = null;
    private BigDecimal salary = null;
    private BigDecimal billingRate = null;
    private SalaryType salaryType = null;
    private Person person = null;
    private CompanyPerson companyPerson = null;
    private boolean voided = false;
	private Set groups = new HashSet();
    
      
    /**
     * @return Returns the id.
     */
    public Long getId() {
        return id;
    }
    /**
     * @param id The id to set.
     */
    public void setId(Long id) {
        this.id = id;
    }
    
    
	/**
	 * @return Returns the companyPerson.
	 */
	public CompanyPerson getCompanyPerson() {
		return companyPerson;
	}
	/**
	 * @param companyPerson The companyPerson to set.
	 */
	public void setCompanyPerson(CompanyPerson companyPerson) {
		this.companyPerson = companyPerson;
	}

	/**
	 * @return Returns the person.
	 */
	public Person getPerson() {
		return person;
	}
	/**
	 * @param person The person to set.
	 */
	public void setPerson(Person person) {
		this.person = person;
	}
    /**
     * @return Returns the role.
     */
    public CompanyPersonRoleType getRoleType() {
        return roleType;
    }
    /**
     * @param role The role to set.
     */
    public void setRoleType(CompanyPersonRoleType roleType) {
        this.roleType = roleType;
    }
    
    
	/**
	 * @return Returns the title.
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * @param title The title to set.
	 */
	public void setTitle(String title) {
		this.title = title;
	}
    /**
     * @return Returns the voided.
     */
    public boolean getVoided() {
        return voided;
    }
    /**
     * @param voided The voided to set.
     */
    public void setVoided(boolean voided) {
        this.voided = voided;
    }
    
    
    /**
     * @return Returns the dateEnd.
     */
    public Date getDateEnd() {
        return dateEnd;
    }
    /**
     * @param dateEnd The dateEnd to set.
     */
    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }
    
    /**
     * @return Returns the dateStart.
     */
    public Date getDateStart() {
        return dateStart;
    }
    /**
     * @param dateStart The dateStart to set.
     */
    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }
    
    
	/**
	 * @return Returns the dateEndExpected.
	 */
	public Date getDateEndExpected() {
		return dateEndExpected;
	}
	/**
	 * @param dateEndExpected The dateEndExpected to set.
	 */
	public void setDateEndExpected(Date dateEndExpected) {
		this.dateEndExpected = dateEndExpected;
	}
	/**
	 * @return Returns the dateStartExpected.
	 */
	public Date getDateStartExpected() {
		return dateStartExpected;
	}
	/**
	 * @param dateStartExpected The dateStartExpected to set.
	 */
	public void setDateStartExpected(Date dateStartExpected) {
		this.dateStartExpected = dateStartExpected;
	}

	public BigDecimal getSalary() {
		return salary;
	}

	public void setSalary(BigDecimal salary) {
		this.salary = salary;
	}


	public SalaryType getSalaryType() {
		return salaryType;
	}

	public void setSalaryType(SalaryType salaryType) {
		this.salaryType = salaryType;
	}
	
	
	/**
	 * @return Returns the billingRate.
	 */
	public BigDecimal getBillingRate() {
		return billingRate;
	}
	/**
	 * @param billingRate The billingRate to set.
	 */
	public void setBillingRate(BigDecimal billingRate) {
		this.billingRate = billingRate;
	}

	/**
	 * @return Returns the groups.
	 */
	public Set getGroups() {
		return groups;
	}

	/**
	 * @param groups The groups to set.
	 */
	public void setGroups(Set groups) {
		this.groups = groups;
	}
	
	public CompanyPersonGroup findGroup(CompanyPersonGroupType groupType) {
		CompanyPersonGroup foundGroup = null;
		Iterator iter = this.groups.iterator();
		while (iter.hasNext()) {
			CompanyPersonGroup group = (CompanyPersonGroup)iter.next();
			if (group == null)
				throw new SystemException("Null group was found on group list");
			if (group.getCompanyPersonGroupType().getCode().equals(groupType.getCode())) {
				foundGroup = group;
			}
		}
		return (foundGroup);
	}
	
	public boolean hasGroupMembership(CompanyPersonGroupType groupType) {
		CompanyPersonGroup group = findGroup(groupType);
		return (group != null);
	}

	public void addGroupMembership(CompanyPersonGroupType groupType) {
		if (groupType == null)
			throw new SystemException("addGroupMembership() was passed a null group type");
		CompanyPersonGroup group = this.findGroup(groupType);
		if (group == null) {
			group = new CompanyPersonGroup();
			group.setCompanyPersonGroupType(groupType);
			group.setCompanyPersonRole(this);
			this.groups.add(group);
		}
	}
	
	public void removeGroupMembership(CompanyPersonGroupType groupType) {
		CompanyPersonGroup group = this.findGroup(groupType);
		if (group != null) {
			this.groups.remove(group);
		}
	}
	
	
}
